module.exports = class extends think.Controller {
  async __before() {
    const options = await think.model('options').loadSystemOptions();
    this.ctx.state.options = options;
    this.checkInstalled();

    // 设置后台模板变量
    this.assign('site_name', 'Thincho');
    this.assign('page_title', this.getPageTitle());
    const isLogin = await this.hasLogin();
    if (!isLogin && this.ctx.controller !== 'login') {
      // 尝试 authCode 方式登录
      let authCode = this.cookie('__thincho_authCode');
      if (!think.isEmpty(authCode)) {
        const user = await think.model('users').where({authCode: authCode}).find();
        await this.session('uid', user.uid);
        const userService = think.service('user', 'common');
        authCode = userService.generateAuthCode();
        await think.model('users').where({uid: user.uid}).update({authCode: authCode});
        const maxAge = 30 * 24 * 3600 * 1000;
        this.cookie('__thincho_uid', user.uid.toString(), {maxAge: maxAge});
        this.cookie('__thincho_authCode', authCode, {maxAge: maxAge});
      } else {
        return this.redirect('/admin/login/index');
      }
    }

    if (this.ctx.controller !== 'login') {
      const uid = await this.session('uid');
      const user = await think.model('users').where({'uid': uid}).find();
      delete user.password;
      delete user.salt;
      this.ctx.state.uid = uid;
      this.ctx.state.user = user;
      this.assign('uid', uid);
      this.assign('user', user);
    }
  }

  checkInstalled() {
    if (this.ctx.state.options.installed <= 0) {
      return this.redirect('/install/index/index');
    }
  }

  /**
   * 判断是否登录
   */
  async hasLogin() {
    const uid = await this.session('uid');
    return !think.isEmpty(uid);
  }

  getPageTitle() {
    let pageTitle = '网站概要';
    switch (this.ctx.controller) {
      case 'index':
        pageTitle = '网站概要';
        break;
      case 'login':
        pageTitle = '欢迎登录';
        break;
      case 'profile':
        pageTitle = '个人设置';
        break;
      case 'backup':
        pageTitle = '备份';
        break;
      case 'write':
        switch (this.ctx.action) {
          case 'post':
            pageTitle = '撰写新文章';
            break;
          case 'page':
            pageTitle = '创建新页面';
            break;
        }
        break;
      case 'manage':
        switch (this.ctx.action) {
          case 'posts':
            pageTitle = '管理文章';
            break;
          case 'pages':
            pageTitle = '管理独立页面';
            break;
          case 'comments':
            pageTitle = '管理评论';
            break;
          case 'categories':
            pageTitle = '管理分类';
            break;
          case 'tags':
            pageTitle = '管理标签';
            break;
          case 'medias':
            pageTitle = '管理文件';
            break;
          case 'users':
            pageTitle = '管理用户';
            break;
        }
        break;
      case 'options':
        switch (this.ctx.action) {
          case 'general':
            pageTitle = '基本设置';
            break;
          case 'discussion':
            pageTitle = '评论设置';
            break;
          case 'reading':
            pageTitle = '阅读设置';
            break;
          case 'permalink':
            pageTitle = '永久链接设置';
            break;
        }
        break;
    }
    return pageTitle;
  }
};
